﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using DAO_DTO_WS.DTO;
using System.Data.SqlClient;


namespace DAO_DTO_WS.DAO
{
    public class LoiBinhDAO : Abstract
    {
        public static List<LoiBinhDTO> LayCommentTheoSp(int maSP)
        {
            SqlConnection connection = KetNoi();
            List<LoiBinhDTO> dsLoiBinh = new List<LoiBinhDTO>();
            String SqlString = "select * from LOIBINH where MaSanPham=@maSp";
            try
            {
                SqlCommand lenh = new SqlCommand(SqlString, connection);
                lenh.Parameters.AddWithValue("@maSp", maSP);
                SqlDataReader Doc = lenh.ExecuteReader();
                while (Doc.Read())
                {
                    LoiBinhDTO loibinh = new LoiBinhDTO();
                    loibinh.MaLoiBinh = (int)Doc[0];
                    loibinh.MaThanhVien = (int)Doc[1];
                    loibinh.MaSanPham = (int)Doc[2];
                    loibinh.NoiDung = Doc[3].ToString();
                    loibinh.TinhTrang = (int)Doc[4];
                    dsLoiBinh.Add(loibinh);
                }
            }
            catch (Exception ex)
            {
                dsLoiBinh = new List<LoiBinhDTO>();
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return dsLoiBinh;
        }
        public static List<LoiBinhDTO> LayCommentTheoThanhVien(int maTV)
        {
            SqlConnection connection = KetNoi();
            List<LoiBinhDTO> dsLoiBinh = new List<LoiBinhDTO>();
            String SqlString = "select * from LOIBINH where MaThanhVien=@maTV";
            try
            {
                SqlCommand lenh = new SqlCommand(SqlString, connection);
                lenh.Parameters.AddWithValue("@maTV", maTV);
                SqlDataReader Doc = lenh.ExecuteReader();
                while (Doc.Read())
                {
                    LoiBinhDTO loibinh = new LoiBinhDTO();
                    loibinh.MaLoiBinh = (int)Doc[0];
                    loibinh.MaThanhVien = (int)Doc[1];
                    loibinh.MaSanPham = (int)Doc[2];
                    loibinh.NoiDung = Doc[3].ToString();
                    loibinh.TinhTrang = (int)Doc[4];
                    dsLoiBinh.Add(loibinh);
                }
            }
            catch (Exception ex)
            {
                dsLoiBinh = new List<LoiBinhDTO>();
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return dsLoiBinh;
        }
        public static bool ThemLoiBinh(int maTV, int maSP, string noidung)
        {
            SqlConnection connection = KetNoi();
            bool KetQua = false;
            String SqlString = "INSERT INTO LOIBINH(MaThanhVien,MaSanPham,NoiDung,TinhTrang) VALUES " +
                 "(@maTV,@maSP,@noidung,@tinhtrang)";
            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@maTV", maTV);
                command.Parameters.AddWithValue("@maSP", maSP);
                command.Parameters.AddWithValue("@noidung", noidung);
                command.Parameters.AddWithValue("@tinhtrang", 1);
                int rows = 0;
                rows = command.ExecuteNonQuery();
                if (rows == 1)
                    KetQua = true;
            }
            catch (Exception ex)
            {
                KetQua = false;
                throw ex;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return KetQua;
        }
    }
}
